<?php
use Slim\Slim;
class Blog
{
	protected $app;

	protected $loader;

	protected $loaded = false;

	protected $posts ;

	public function __construct(Slim $app, $autoload = false)
	{
		$path = $app->config("posts.path");
		if ( is_null($path) ) 
		{
			$path = __DIR__."../paths";

			if ( !is_dir($path) ) 
			{
				die("posts dir is not exists");
			}

			$app->config("posts.path", $path);
		}

		$this->app = $app;
		$this->setLoader(new Loader($path));
		return $this;
	}

	public function loadFiles($forcus = false)
	{
		$this->loader->load();
		$this->app->applyHook("files.loaded", $this->loader->files());
	}

	public function loaded()
	{
		return $this->loaded;
	}
	public function posts($callback = NULL)
	{
		if ( !$this->loaded() ) 
		{
			$this->loadFiles();
		}

		$files = $this->loader->files();

		foreach ( $files as $file ) 
		{
			$post = array();
			list($year, $month, $day, $url_title) = sscanf($file, '%d-%d-%d-%s');
			$timestamp = mktime(0,0,0, $month, $day, $year);
			$url_title = strstr($url_title,'.', true);

			if ( isset($this->posts[$url_title]) ) 
			{
				continue;
			}

			$content = $this->loader->get_content($file);
			list($head, $body) = $this->parse($content);
			// init 
			$post['title'] = $head ? $this->getAttribute("title", $head) : "";
			$post['category'] = $head ? $this->getAttribute("category", $head) : "";
			$post['tags'] = $head ? $this->getAttribute("tags", $head) : "";
			$post['date'] = $head ? $this->getAttribute("date", $head) : '';
			$post['body'] = Helper::markdown($body);
			$post['desc'] = mb_substr($post['body'], 0, 80);

			if ( $callback ) 
			{
				$post = $callback($post);
			}
			$this->posts[$url_title] = $post;
		}

		$this->loaded = true;
		//printf("<pre>%s</pre>",print_r($this->posts, true));
		return $this->posts;
	}

	protected function parse($content)
	{
		$head = $body = '';
		$need = "---";
		if ( ($pos = strrpos($content, $need)) !== false ) 
		{
			$head = substr($content, strlen($need), $pos - strlen($need));
			$body = substr($content, $pos + strlen($need));
		}
		return array($head, $body);
	}

	public function findPost($title, $default=NULL)
	{
		if ( !$this->loaded() ) 
		{
			//初始化posts
		}

		foreach((array)$title as $name)
		{
			if ( isset($this->posts[$name]) ) 
			{
				return $this->posts[$name];
			}
		}
		return $default instanceof Clousur ? $default() : $default;
	}

	public function getAttribute($attr, $text)
	{
		$method = "get{$attr}";

		if ( method_exists($this, $method) ) 
		{
			return $this->$method($text);
		}

		return '';
	}

	//获取文章标题
	public function getTitle($text)
	{
		preg_match("#title:\s?(.*)#", $text, $match);

		if ( !empty($match) ) 
		{
			return $match[1];
		}
	}

	//获取文章分类
	public function getCategory($text)
	{
		preg_match("#category:\s?(.*)#", $text, $match);

		if ( !empty($match) ) 
		{
			if ( false === strpos($match[1], ',') ) 
			{
				return array($match[1]);
			}

			return explode(',', $match[1]);
		}
	}
	//获取文章标签
	public function getDate($text)
	{
		preg_match("#date:\s?(.*)#", $text, $match);

		if ( !empty($match) ) 
		{
			list($year, $month, $day) = explode("-", $match[1]);

			return mktime(0, 0, 0, $month, $day, $year);
		}
	}
	
	public function setLoader(Loader $loader)
	{
		$this->loader = $loader;

		return $this;
	}
	public function getLoader()
	{
		return $this->loader;
	}

	public function setApp($app)
	{
		$this->app = $app;
	}

	public function getApp()
	{
		return $this->app;
	}
}